# 导包
from api.login import LoginAPI
from api.staff import StaffAPI
import config
from common.assertutil import common_assert
from common.dbutil import DBUtil


# 创建测试类
class TestStaffAPI:
    # 初始化
    token = None
    staff_id = None

    # 类前置处理
    def setup_class(self):
        # 实例化接口对象
        self.login_api = LoginAPI()
        self.staff_api = StaffAPI()
        # 登录成功
        response = self.login_api.login(config.LOGIN_DATA)
        # 提取登录token保存至类的属性
        TestStaffAPI.token = response.json().get("data")
        print(TestStaffAPI.token)
        # 清理测试数据
        # 删除手机号存在的员工记录
        sql = "delete from bs_user where mobile='13611335505';"
        print(DBUtil().exe_sql(sql))

    # 类后置处理
    def teardown_class(self):
        pass

    # 前置处理
    def setup(self):
        pass

    # 后置处理
    def teardown(self):
        pass

    # step01：添加员工成功
    def test01_insert_staff(self):
        test_data = {
            "username": "jack6660001",
            "mobile": "13611335505",    # 手机号必须唯一
            "workNumber": "952710001"
        }
        response = self.staff_api.insert_staff(test_data, TestStaffAPI.token)
        print(response.json())
        common_assert(response)
        # 提取员工id并保存至类的属性
        TestStaffAPI.staff_id = response.json().get("data").get("id")
        print("-" * 80)
        print(TestStaffAPI.staff_id)

    # step02：查询员工成功
    def test02_select_staff(self):
        response = self.staff_api.select_staff(TestStaffAPI.token, TestStaffAPI.staff_id)
        common_assert(response)

    # step03：修改员工成功
    def test03_update_staff(self):
        response = self.staff_api.update_staff(TestStaffAPI.token, TestStaffAPI.staff_id, {"username": "rose777"})
        common_assert(response)

    # step04: 删除员工成功
    def test04_delete_staff(self):
        response = self.staff_api.delete_staff(TestStaffAPI.token, TestStaffAPI.staff_id)
        common_assert(response)
